package org.cying.main.service;

import org.cying.main.dao.CreateDept;
import org.cying.main.dao.DeleteDept;
import org.cying.main.dao.QueryDept;
import org.cying.main.dao.UpdateDept;
import org.cying.main.domain.Department;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门映射接口控制类
 * @author Cying
 */
@Service
public class DeptService {
    
    @Resource
    private QueryDept query;
    
    @Resource
    private CreateDept create;

    @Resource
    private UpdateDept update;
    
    @Resource
    private DeleteDept delete;
    
    public List<Department> getAllDept(String tid) {
        return query.getAllDept(tid);
    }
    
    public int createDept(String name) {
        return create.createDept(name);
    }
    
    public int createInit(String name) {
        return create.createInit(name);
    }
    
    public boolean createGroup(String department, String group) {
        Map<String, String> map = new HashMap<>();
        map.put("department", department);
        map.put("group", group);
        return create.createGroup(map) == 1;
    }
    
    public int alterDept(String oldName, String newName) {
        Map<String, Object> map = new HashMap<>();
        map.put("oldName", oldName);
        map.put("newName", newName);
        return update.alterDept(map);
    }
    
    public boolean alterGroup(String oldName, String newName) {
        Map<String, Object> map = new HashMap<>();
        map.put("oldName", oldName);
        map.put("newName", newName);
        return update.alterGroup(map) > 0;
    }
    
    public boolean deleteDept(String name) {
        return delete.deleteDept(name) == 1;
    }
    
    public boolean deleteGroup(String department, String group) {
        Map<String, String> map = new HashMap<>();
        map.put("department", department);
        map.put("group", group);
        int result = delete.deleteGroup(map);
        System.out.println(result);
        return result == 1;
//        return true;
    }

    public boolean moveGroup(String oldDepartment, String newDepartment, String group) {
        Map<String, Object> map = new HashMap<>();
        map.put("oldDepartment", oldDepartment);
        map.put("newDepartment", newDepartment);
        map.put("group", group);
        return update.moveGroup(map) == 1;
    }

}
